//
//  TitleScrollViewController.m
//  song-git
//
//  Created by song on 16/2/20.
//  Copyright © 2016年 song. All rights reserved.
//

#import "TitleScrollViewController.h"
#import "HMSegmentedControl.h"

@interface TitleScrollViewController () <UIScrollViewDelegate>

@property(nonatomic, strong)HMSegmentedControl *titleSegment;

@property (nonatomic, assign) CGFloat  sizeWidth;
@property (nonatomic, assign) CGFloat  sizeHeight;
@property (nonatomic, assign) CGFloat  heightForTopView;
@property (nonatomic, assign) CGFloat  heightForTitle;
@property(nonatomic , strong) UIScrollView *scrollView;


@end

@implementation TitleScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.view.backgroundColor =[UIColor whiteColor];
}

-(id)initWithTitle:(NSString *)title andSubTitles:(NSArray *)titles andSubControllers:(NSArray *)controllers andUnderTarbar:(BOOL)underTarbar andUserPortrait:(BOOL)userPortrait
{
    
    self = [super init];
    if(self)
    {
        _sizeWidth  =self.view.frame.size.width;
        _sizeHeight = self.view.frame.size.height;
        _heightForTitle =35;
        [self initSubviewForTitleSement:titles];
        [self initSubViewForControllers:controllers];
        
        if (underTarbar) {
            _heightForTopView = 49;
        }else{
            _heightForTopView =0;
        }
    }
    
    return  self;
}

-(void)initSubviewForTitleSement:(NSArray *)titles
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    _titleSegment = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 35)];
    [_titleSegment setSectionTitles:titles];
    [_titleSegment setSelectedSegmentIndex:0];
    [_titleSegment setSelectionStyle:HMSegmentedControlSelectionStyleBox];
    [_titleSegment setSelectionIndicatorLocation:HMSegmentedControlSelectionIndicatorLocationDown];

    [self.view addSubview:_titleSegment];
    __weak typeof(self) weakSelf = self;//点击滚动标题的动作
    [_titleSegment setIndexChangeBlock:^(NSInteger index) {
        
        [weakSelf.scrollView scrollRectToVisible:CGRectMake(weakSelf.sizeWidth * index, weakSelf.heightForTitle, weakSelf.sizeWidth, weakSelf.sizeHeight-_heightForTitle-_heightForTopView) animated:YES];
    }];
}

-(void)initSubViewForControllers:(NSArray *)controllers
{
    _scrollView  =[[UIScrollView alloc] initWithFrame:CGRectMake(0, _heightForTitle, self.sizeWidth, _sizeHeight -_heightForTitle - _heightForTopView) ];
    [_scrollView setPagingEnabled:YES];
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    [_scrollView setBounces:NO];
    [_scrollView setContentSize:CGSizeMake(_sizeWidth * controllers.count, _sizeHeight -_heightForTitle - _heightForTopView)];
    [_scrollView scrollRectToVisible:CGRectMake(0, _heightForTitle, _sizeWidth, _sizeHeight-_heightForTitle-_heightForTopView) animated:YES];
    
    _scrollView.delegate  = self;
    [self.view addSubview:_scrollView];
    int i =0 ;
    for (UIViewController *controller in controllers) {
        controller.view.frame  = CGRectMake(_sizeWidth * i, 0, _sizeWidth, _sizeHeight -_heightForTitle - _heightForTopView);
        controller.view.autoresizingMask  =UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth ;
        [_scrollView addSubview:controller.view];
        [self addChildViewController:controller];
        i++;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWith = scrollView.frame.size.width;
    NSInteger page  = scrollView.contentOffset.x / pageWith;
    [_titleSegment setSelectedSegmentIndex:page animated:YES];
}

@end
